<?php
/**
 * Created by PhpStorm.
 * User: sunshine
 * Date: 17-5-9
 * Time: 上午2:01
 */

namespace Admin\Controller;


use Think\Controller;

class StudentController extends Controller
{
    public function ajaxaddstudent()
    {
        $userInfo = GetUserInfo();
        $postData = I('post.');
        if ($userInfo['status']) {
            //判断权限
            if ($userInfo['info']['userprivilege'] == 10) {
                if ($postData) {
                    //这里需要补充判断语句
                    $studentmodel = new \Admin\Model\StudentModel();
                    $student = array(
                        'sid' => $postData['studentid'],
                        'stuName' => $postData['studentname'],
                        'stuSex' => $postData['sex'],
                        'stuBorn' => $postData['borndate'],
                        'stuCollege' => $postData['college'],
                        'stuSpecialty' => $postData['specialty'],
                        'stuClass' => $postData['studentclass'],
                        'stuPolitics' => $postData['politics'],
                        'stuMinority' => $postData['minority'],
                        'stuTel' => $postData['studenttel'],
                        'stuEmail' => $postData['studentemail'],
                        'stuLocation' => $postData['studentlocation']
                    );
                    $result = $studentmodel->addStudent($student);
                    echo json_encode($result);
                } else {
                    echo json_encode(array(
                        'status' => false,
                        'errmsg' => 'invalid data',
                        'errcode' => 109
                    ));
                }
            } else {
                echo json_encode(array(
                    'status' => false,
                    'errmsg' => 'no privilege',
                    'errcode' => 108
                ));
            }
        } else {
            header('Location: ' . U('Admin/User/login'));
        }
    }

    public function ajaxfindstudent()
    {
        $userInfo = GetUserInfo();
        $postData = I('post.');
        if ($userInfo['status']) {
            //判断权限
            if ($userInfo['info']['userprivilege'] == 10) {
                if ($postData['sid']) {
                    //这里需要补充判断语句
                    $studentmodel = new \Admin\Model\StudentModel();
                    $result = $studentmodel->findStudent($postData['sid'], $postData['index']);
                    echo json_encode($result);
                } else {
                    echo json_encode(array(
                        'status' => false,
                        'errmsg' => 'invalid data',
                        'errcode' => 109,
                        'info' => $postData
                    ));
                }
            } else {
                echo json_encode(array(
                    'status' => false,
                    'errmsg' => 'no privilege',
                    'errcode' => 108
                ));
            }
        } else {
            header('Location: ' . U('Admin/User/login'));
        }
    }

    public function ajaxdeletestudent(){
        $userInfo = GetUserInfo();
        $postData = I('post.');
        if ($userInfo['status']) {
            //判断权限
            if ($userInfo['info']['userprivilege'] == 10) {
                if ($postData['sid']) {
                    //这里需要补充判断语句
                    $studentmodel = new \Admin\Model\StudentModel();
                    $result = $studentmodel->deleteStudent($postData['sid']);
                    echo json_encode($result);
                } else {
                    echo json_encode(array(
                        'status' => false,
                        'errmsg' => 'invalid data',
                        'errcode' => 109,
                        'info' => $postData
                    ));
                }
            } else {
                echo json_encode(array(
                    'status' => false,
                    'errmsg' => 'no privilege',
                    'errcode' => 108
                ));
            }
        } else {
            header('Location: ' . U('Admin/User/login'));
        }
    }

    public function ajaxgetstudent(){
        $userInfo = GetUserInfo();
        $postData = I('post.');
        if ($userInfo['status']) {
            //判断权限
            if ($userInfo['info']['userprivilege'] == 10) {
                if ($postData['sid']) {
                    //这里需要补充判断语句
                    $studentmodel = new \Admin\Model\StudentModel();
                    $result = $studentmodel->getStudent($postData['sid']);
                    echo json_encode($result);
                } else {
                    echo json_encode(array(
                        'status' => false,
                        'errmsg' => 'invalid data',
                        'errcode' => 109,
                        'info' => $postData
                    ));
                }
            } else {
                echo json_encode(array(
                    'status' => false,
                    'errmsg' => 'no privilege',
                    'errcode' => 108
                ));
            }
        } else {
            header('Location: ' . U('Admin/User/login'));
        }
    }

    public function ajaxchangestudent(){
        $userInfo = GetUserInfo();
        $postData = I('post.');
        if ($userInfo['status']) {
            //判断权限
            if ($userInfo['info']['userprivilege'] == 10) {
                if ($postData) {
                    //这里需要补充判断语句
                    $studentmodel = new \Admin\Model\StudentModel();
                    $student = array(
                        'sid' => $postData['studentid'],
                        'stuName' => $postData['studentname'],
                        'stuSex' => $postData['sex'],
                        'stuBorn' => $postData['borndate'],
                        'stuCollege' => $postData['college'],
                        'stuSpecialty' => $postData['specialty'],
                        'stuClass' => $postData['studentclass'],
                        'stuPolitics' => $postData['politics'],
                        'stuMinority' => $postData['minority'],
                        'stuTel' => $postData['studenttel'],
                        'stuEmail' => $postData['studentemail'],
                        'stuLocation' => $postData['studentlocation']
                    );
                    $result = $studentmodel->updateStudent($student);
                    echo json_encode($result);
                } else {
                    echo json_encode(array(
                        'status' => false,
                        'errmsg' => 'invalid data',
                        'errcode' => 109
                    ));
                }
            } else {
                echo json_encode(array(
                    'status' => false,
                    'errmsg' => 'no privilege',
                    'errcode' => 108
                ));
            }
        } else {
            header('Location: ' . U('Admin/User/login'));
        }
    }

    function ajaxgetcollege(){
        $studentcollegemodel = new \Admin\Model\StudentModel();
        echo json_encode($studentcollegemodel->getCollege());
    }

    function  ajaxgetspecialty(){
        $postData = I('post.cid');
        if ($postData){
            $studentcollegemodel = new \Admin\Model\StudentModel();
            echo json_encode($studentcollegemodel->getSpecialty($postData));
        }
    }
}